package com.iamteer.day23;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UdpReceive {

	public static void main(String[] args) throws IOException {
		udpReceive();
	}
	public static void udpReceive() throws IOException {
		//1. 建立 UDP 服务，并且监听一个端口
		DatagramSocket socket = new DatagramSocket(9090);
		//2. 准备缓冲区用于接收数据
		byte[] buf = new byte[1024];
		//3. 调用 UDP 服务接收数据包
		DatagramPacket packet = new DatagramPacket(buf, buf.length);
		socket.receive(packet);
		System.out.println(new String(buf, 0, packet.getLength()));
		//4. 关闭资源
		socket.close();
	}
}
